package com.jt.www.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 描述：计算工具类
 * 类名称：MathUtil.java
 * 作者： wangyang
 * 版本：1.0
 * 修改： 2018/5/11 16:00
 * 创建日期： 2018/5/11 16:00
 * 版权：江泰保险经纪股份有限公司
 */
public class MathUtil {

    public static final Logger log = LoggerFactory.getLogger(MathUtil.class);

    /**
     * 转换Object对象为BigDecimal类型
     *
     * @param value 被转换的值
     * @return
     */
    public static BigDecimal getBigDecimal(Object value) {
        BigDecimal ret = null;
        if (value != null) {
            if (value instanceof BigDecimal) {
                ret = (BigDecimal) ((BigDecimal) value).setScale(4, BigDecimal.ROUND_HALF_EVEN);
            } else if (value instanceof String) {
                ret = new BigDecimal((String) value).setScale(4, BigDecimal.ROUND_HALF_EVEN);
            } else if (value instanceof BigInteger) {
                ret = new BigDecimal((BigInteger) value).setScale(4, BigDecimal.ROUND_HALF_EVEN);
            } else if (value instanceof Number) {
                ret = new BigDecimal(((Number) value).doubleValue()).setScale(4, BigDecimal.ROUND_HALF_EVEN);
            } else if (value instanceof BigDecimal[]) {
                int length = Array.getLength(value);
                Object[] os = new Object[length];
                for (int i = 0; i < os.length; i++) {
                    os[i] = Array.get(value, i);
                }
                BigDecimal object = (BigDecimal) os[0];
                return object.setScale(4, BigDecimal.ROUND_HALF_EVEN);
            } else if (value instanceof int[]) {
                int length = Array.getLength(value);
                Object[] os = new Object[length];
                for (int i = 0; i < os.length; i++) {
                    os[i] = Array.get(value, i);
                }
                BigDecimal object = new BigDecimal((int) os[0]);
                return object.setScale(4, BigDecimal.ROUND_HALF_EVEN);
            } else {
                log.error("数值转换异常");
                throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
            }
        }
        return ret;
    }

    /**
     * 转换Object对象为BigDecimal类型
     *
     * @param value 被转换的值
     * @return
     */
    public static BigDecimal getBigDecimal(Object value, int scale) {
        BigDecimal ret = null;
        if (value != null) {
            if (value instanceof BigDecimal) {
                ret = (BigDecimal) ((BigDecimal) value).setScale(scale, BigDecimal.ROUND_HALF_UP);
            } else if (value instanceof String) {
                ret = new BigDecimal((String) value).setScale(scale, BigDecimal.ROUND_HALF_UP);
            } else if (value instanceof BigInteger) {
                ret = new BigDecimal((BigInteger) value).setScale(scale, BigDecimal.ROUND_HALF_UP);
            } else if (value instanceof Number) {
                ret = new BigDecimal(((Number) value).doubleValue()).setScale(scale, BigDecimal.ROUND_HALF_UP);
            } else if (value instanceof BigDecimal[]) {
                int length = Array.getLength(value);
                Object[] os = new Object[length];
                for (int i = 0; i < os.length; i++) {
                    os[i] = Array.get(value, i);
                }
                BigDecimal object = (BigDecimal) os[0];
                return object.setScale(scale, BigDecimal.ROUND_HALF_UP);
            } else if (value instanceof int[]) {
                int length = Array.getLength(value);
                Object[] os = new Object[length];
                for (int i = 0; i < os.length; i++) {
                    os[i] = Array.get(value, i);
                }
                BigDecimal object = new BigDecimal((int) os[0]);
                return object.setScale(scale, BigDecimal.ROUND_HALF_UP);
            } else {
                log.error("数值转换异常");
                throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigDecimal.");
            }
        }
        return ret;
    }

    public static <T> T[] convertArray(Class<T> targetType, Object[] arrayObjects) {
        if (targetType == null) {
            return (T[]) arrayObjects;
        }
        if (arrayObjects == null) {
            return null;
        }
        T[] targetArray = (T[]) Array.newInstance(targetType, arrayObjects.length);
        try {
            System.arraycopy(arrayObjects, 0, targetArray, 0, arrayObjects.length);
        } catch (ArrayStoreException e) {
            e.printStackTrace();
        }
        return targetArray;
    }
}
